package cn.maidouya.user.controller;

import cn.maidouya.basic.exception.BusinessExcepiton;
import cn.maidouya.basic.util.AjaxResult;
import cn.maidouya.basic.util.PageList;
import cn.maidouya.user.domain.User;
import cn.maidouya.user.dto.UserDto;
import cn.maidouya.user.query.UserQuery;
import cn.maidouya.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(tags = "登录接口",description = "登录接口详细描述")
public class UserController {
    @Autowired
    private IUserService userService;

    /**
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查找登录",notes = "根据ID查找登录")
    public User loadById(@PathVariable Long id) {
        User one = userService.loadById(id);
        return one;
    }
    @GetMapping
    @ApiOperation(value = "查找全部登录",notes = "查找所有登录信息")
    public List<User> loadAll() {
        List<User> list = userService.loadAll();
        return list;
    }
    @PostMapping
    @ApiOperation(value = "查找登录分页数据",notes = "分页查找所有登录信息")
    public PageList<User> queryData(@RequestBody UserQuery userQuery) {
        PageList<User> pageList = userService.queryData(userQuery);
        return pageList;
    }
    @PutMapping
    @ApiOperation(value = "添加和修改登录",notes = "添加和修改登录")
    public AjaxResult saveAndUpdate(@RequestBody User user) {
                try {
                    if (user.getId() == null){
                        userService.save(user);
                    } else {
                        userService.update(user);
                    }
                    return new AjaxResult();
                } catch (Exception e) {
                    e.printStackTrace();
                    return  AjaxResult.getResult().setSuccess(false).setMessage("操作失败");
                }
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除登录",notes = "根据id删除登录")
    public AjaxResult delete(@PathVariable Long id) {
        try {
          userService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.getResult().setSuccess(false).setMessage("删除失败");
        }
    }

    /**
     *   注册手机号码登录
     * @param userDto
     * @return
     */
    @PostMapping("/register/phone")
    public AjaxResult registerPhone(@RequestBody UserDto userDto){
        try {
            userService.registerPhoneUser(userDto);
            return AjaxResult.getResult();
        } catch (BusinessExcepiton e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage(e.getMessage());//获取service业务异常
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setMessage("系统繁忙，请稍后再试！！");
        }
    }
    /**
     *   邮箱注册
     * @param userDto
     * @return
     */
    @PostMapping("/email")
    public AjaxResult registeremail (@RequestBody UserDto userDto){
        System.err.println("邮箱注册：" +userDto);
        try {
           userService.registeremail(userDto);
            return AjaxResult.getResult();
        } catch (BusinessExcepiton e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setMessage("系统繁忙，请稍后重试！！");
        }
    }

}